package autoLoginBusiness;

import models.MyConfig;
import models.MySleep;
import models.ReturnJson;
import models.TestServerBusinessImpl;

import javax.servlet.http.HttpServletRequest;
import javax.swing.*;
import java.awt.*;

/**
 * Created by yangxianming on 2021/4/22.
 */
public class QrCodeLogin implements TestServerBusinessImpl {
    @Override
    public String doIt(HttpServletRequest request, MyConfig myConfig) {
        ReturnJson returnJson = new ReturnJson();
        String fileName = request.getParameter("fileName");
        System.out.println(fileName);

        //弹出二维码
        String picZoom = myConfig.getPropertiesConfig().getProperties().getProperty("picZoom");
        String imagePath = myConfig.getAppPath() + "//images//";
        JFrame jFrame = myConfig.getjFrame();
        ImageIcon imageIcon = new ImageIcon(imagePath + fileName, "p1");
        JLabel label = new JLabel(change(imageIcon, Double.valueOf(picZoom)));
        jFrame.add(label);
        jFrame.setVisible(true);
        MySleep.sleep(2000);

        //操作手机登陆支付宝登陆
        String[] opInterval = myConfig.getPropertiesConfig().getProperties().getProperty("opInterval").split(";");
        String adbPath = myConfig.getPropertiesConfig().getProperties().getProperty("adbPath") + "\\";
        String tap1 = myConfig.getPropertiesConfig().getProperties().getProperty("tap1");
        String tap2 = myConfig.getPropertiesConfig().getProperties().getProperty("tap2");
        Runtime runtime = Runtime.getRuntime();
        try {
            runtime.exec(adbPath + "adb shell input keyevent 26");
            System.out.println("唤醒手机");
            Thread.sleep(Long.valueOf(opInterval[0]));
            runtime.exec(adbPath + "adb shell am start com.eg.android.AlipayGphone/.AlipayLogin");
            System.out.println("激活支付宝");
            Thread.sleep(Long.valueOf(opInterval[1]));
            runtime.exec(adbPath + "adb shell input tap " + tap1);
            System.out.println("点击扫码");
            Thread.sleep(Long.valueOf(opInterval[2]));
            runtime.exec(adbPath + "adb shell input tap " + tap2);
            System.out.println("确认登陆");
            Thread.sleep(Long.valueOf(opInterval[3]));
            runtime.exec(adbPath + "adb shell input keyevent 26");
            System.out.println("息屏");

        }catch (Exception e){
            e.printStackTrace();
        }
        jFrame.remove(label);
        jFrame.revalidate();

        returnJson.setResult("ok");
        return returnJson.toString();
    }

    private ImageIcon change(ImageIcon image,double i){
        int width=(int) (image.getIconWidth()*i);
        int height=(int) (image.getIconHeight()*i);
        Image img=image.getImage().getScaledInstance(width, height, Image.SCALE_DEFAULT);//第三个值可以去查api是图片转化的方式
        return new ImageIcon(img);
    }
}
